package org.example;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

public class NettyClientHandler extends ChannelInboundHandlerAdapter {
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        // 处理接收到的消息，这里 msg 已经被 StringDecoder 解码为字符串
        String message = (String) msg;
        System.out.println("服务器收到消息：" + message);

        // 回复客户端消息
        ctx.writeAndFlush("服务器回应：" + message);
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        // 处理异常情况
        cause.printStackTrace();
        ctx.close(); // 关闭连接
    }
}